package com.java110.mall.cart.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.storeOrderDiscount.IDeleteStoreOrderDiscountBMO;
import com.java110.mall.cart.bmo.storeOrderDiscount.IGetStoreOrderDiscountBMO;
import com.java110.mall.cart.bmo.storeOrderDiscount.ISaveStoreOrderDiscountBMO;
import com.java110.mall.cart.bmo.storeOrderDiscount.IUpdateStoreOrderDiscountBMO;
import com.java110.dto.storeOrder.StoreOrderDiscountDto;
import com.java110.po.storeOrder.StoreOrderDiscountPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "/storeOrderDiscount")
public class StoreOrderDiscountApi {

    @Autowired
    private ISaveStoreOrderDiscountBMO saveStoreOrderDiscountBMOImpl;
    @Autowired
    private IUpdateStoreOrderDiscountBMO updateStoreOrderDiscountBMOImpl;
    @Autowired
    private IDeleteStoreOrderDiscountBMO deleteStoreOrderDiscountBMOImpl;

    @Autowired
    private IGetStoreOrderDiscountBMO getStoreOrderDiscountBMOImpl;

    /**
     * 微信保存消息模板
     * @serviceCode /storeOrderDiscount/saveStoreOrderDiscount
     * @path /app/storeOrderDiscount/saveStoreOrderDiscount
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/saveStoreOrderDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> saveStoreOrderDiscount(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "discountId", "请求报文中未包含discountId");
        Assert.hasKeyAndValue(reqJson, "discountType", "请求报文中未包含discountType");
        Assert.hasKeyAndValue(reqJson, "discountRate", "请求报文中未包含discountRate");
        Assert.hasKeyAndValue(reqJson, "discountPrice", "请求报文中未包含discountPrice");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "discountObjType", "请求报文中未包含discountObjType");
        Assert.hasKeyAndValue(reqJson, "discountObjId", "请求报文中未包含discountObjId");


        StoreOrderDiscountPo storeOrderDiscountPo = BeanConvertUtil.covertBean(reqJson, StoreOrderDiscountPo.class);
        return saveStoreOrderDiscountBMOImpl.save(storeOrderDiscountPo);
    }

    /**
     * 微信修改消息模板
     * @serviceCode /storeOrderDiscount/updateStoreOrderDiscount
     * @path /app/storeOrderDiscount/updateStoreOrderDiscount
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/updateStoreOrderDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> updateStoreOrderDiscount(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "discountId", "请求报文中未包含discountId");
        Assert.hasKeyAndValue(reqJson, "discountType", "请求报文中未包含discountType");
        Assert.hasKeyAndValue(reqJson, "discountRate", "请求报文中未包含discountRate");
        Assert.hasKeyAndValue(reqJson, "discountPrice", "请求报文中未包含discountPrice");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "discountObjType", "请求报文中未包含discountObjType");
        Assert.hasKeyAndValue(reqJson, "discountObjId", "请求报文中未包含discountObjId");
        Assert.hasKeyAndValue(reqJson, "discountId", "discountId不能为空");


        StoreOrderDiscountPo storeOrderDiscountPo = BeanConvertUtil.covertBean(reqJson, StoreOrderDiscountPo.class);
        return updateStoreOrderDiscountBMOImpl.update(storeOrderDiscountPo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /storeOrderDiscount/deleteStoreOrderDiscount
     * @path /app/storeOrderDiscount/deleteStoreOrderDiscount
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/deleteStoreOrderDiscount", method = RequestMethod.POST)
    public ResponseEntity<String> deleteStoreOrderDiscount(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "discountId", "discountId不能为空");


        StoreOrderDiscountPo storeOrderDiscountPo = BeanConvertUtil.covertBean(reqJson, StoreOrderDiscountPo.class);
        return deleteStoreOrderDiscountBMOImpl.delete(storeOrderDiscountPo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /storeOrderDiscount/queryStoreOrderDiscount
     * @path /app/storeOrderDiscount/queryStoreOrderDiscount
     * @param shopId 小区ID
     * @return
     */
    @RequestMapping(value = "/queryStoreOrderDiscount", method = RequestMethod.GET)
    public ResponseEntity<String> queryStoreOrderDiscount(@RequestParam(value = "shopId") String shopId,
                                                      @RequestParam(value = "cartId",required = false) String cartId,
                                                      @RequestParam(value = "orderId",required = false) String orderId,
                                                      @RequestParam(value = "page") int page,
                                                      @RequestParam(value = "row") int row) {
        StoreOrderDiscountDto storeOrderDiscountDto = new StoreOrderDiscountDto();
        storeOrderDiscountDto.setPage(page);
        storeOrderDiscountDto.setRow(row);
        storeOrderDiscountDto.setShopId(shopId);
        if(StringUtil.isEmpty(cartId)){
            storeOrderDiscountDto.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_ORDER);
            storeOrderDiscountDto.setDiscountObjId(orderId);
        }else{
            storeOrderDiscountDto.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_CART);
            storeOrderDiscountDto.setDiscountObjId(cartId);
        }
        return getStoreOrderDiscountBMOImpl.get(storeOrderDiscountDto);
    }
}
